package com.xuelang.pipeline.app.resource.impl;

import com.xuelang.jadp.client.sys.dto.UserInfoDTO;
import com.xuelang.pipeline.app.resource.CloudEnvironmentService;
import com.xuelang.pipeline.app.resource.converter.CloudEnvironmentDTOToCloudEnvironmentConverter;
import com.xuelang.pipeline.client.common.constant.Constant;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.common.util.BizAssert;
import com.xuelang.pipeline.client.resource.dto.CloudEnvironmentDTO;
import com.xuelang.pipeline.client.resource.dto.CloudEnvironmentQuery;
import com.xuelang.pipeline.domain.resource.CloudEnvironment;
import com.xuelang.pipeline.domain.resource.service.CloudEnvironmentDomainService;
import com.xuelang.sys.app.sys.UserManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 多云环境服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class CloudEnvironmentServiceImpl implements CloudEnvironmentService {

    @Autowired
    CloudEnvironmentDomainService cloudEnvironmentDomainService;
    @Autowired
    UserManagerService userManagerService;

    private CloudEnvironmentDTO fullfillCreator(CloudEnvironmentDTO cloudEnvironmentDTO){
        UserInfoDTO user = userManagerService.getUserById(cloudEnvironmentDTO.getCreatorId());
        if (Objects.nonNull(user)) {
            cloudEnvironmentDTO.setCreator(user.getRealName());
        }else{
            cloudEnvironmentDTO.setCreator(Constant.UNKNOWN_USER);
        }
        return cloudEnvironmentDTO;
    }

    @Override
    public PageRecord<CloudEnvironmentDTO> pageList(PageParam<CloudEnvironmentQuery> dtoPageParam) {
        CloudEnvironmentQuery condition = dtoPageParam.getCondition();
        condition = condition == null ? new CloudEnvironmentQuery() : condition;
        PageRecord<CloudEnvironment> pageRecord = cloudEnvironmentDomainService.pageList(
                dtoPageParam.getCurrentPage(),
                dtoPageParam.getPageSize(),
                condition);
        return new PageRecord<>(
                pageRecord.getCurrentPage(),
                pageRecord.getPageSize(),
                pageRecord.getTotalCount(),
                pageRecord.getTotalPages(),
                pageRecord.getRecords().stream()
                        .map(CloudEnvironmentDTOToCloudEnvironmentConverter.INSTANCE::cloudEnvironmentToCloudEnvironmentDTO)
                        .map(this::fullfillCreator)
                        .collect(Collectors.toList())
        );
    }

    @Override
    public List<CloudEnvironmentDTO> list(CloudEnvironmentQuery cloudEnvironmentQuery) {
        return cloudEnvironmentDomainService.list(cloudEnvironmentQuery).stream()
                .map(CloudEnvironmentDTOToCloudEnvironmentConverter.INSTANCE::cloudEnvironmentToCloudEnvironmentDTO)
                .map(this::fullfillCreator)
                .collect(Collectors.toList());
    }


    @Override
    public Boolean add(CloudEnvironmentDTO cloudEnvironmentDTO) {
        BizAssert.notNull(cloudEnvironmentDTO.getName(), "name不能为空");
        CloudEnvironment cloudEnvironment = CloudEnvironmentDTOToCloudEnvironmentConverter.INSTANCE.cloudEnvironmentDTOToCloudEnvironment(cloudEnvironmentDTO);

        return cloudEnvironmentDomainService.save(cloudEnvironment);
    }

    @Override
    public Boolean update(CloudEnvironmentDTO cloudEnvironmentDTO) {
        BizAssert.notNull(cloudEnvironmentDTO.getId(), "id不能为空");
        CloudEnvironment cloudEnvironment = CloudEnvironmentDTOToCloudEnvironmentConverter.INSTANCE.cloudEnvironmentDTOToCloudEnvironment(cloudEnvironmentDTO);
        return cloudEnvironmentDomainService.update(cloudEnvironment);
    }

    @Override
    public Boolean delete(Long id) {
        BizAssert.notNull(id, "id不能为空");
        return cloudEnvironmentDomainService.delete(id);
    }

    @Override
    public CloudEnvironmentDTO query(Long id) {
        BizAssert.notNull(id, "id不能为空");
        CloudEnvironment cloudEnvironment = cloudEnvironmentDomainService.query(id);
        return CloudEnvironmentDTOToCloudEnvironmentConverter.INSTANCE.cloudEnvironmentToCloudEnvironmentDTO(cloudEnvironment);
    }
}
